//
//  ViewController.m
//  YCDynamicNavigationBarDemo
//
//  Created by Jim on 2018/8/13.
//  Copyright © 2018年 youcash. All rights reserved.
//

#import "ViewController.h"
#import "YCDynamicNavigationBar.h"

#define kSCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define kSCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface ViewController () <YCDynamicNavigationBarDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) YCDynamicNavigationBar *bar;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor purpleColor];
    self.navigationController.navigationBar.hidden = YES;
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT)];
    scrollView.backgroundColor = [UIColor lightGrayColor];
    scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height * 2);
    scrollView.contentInset = UIEdgeInsetsMake(0, 0, 200, 0);
    scrollView.delegate = self;
    if (@available(iOS 11.0, *)) {
        scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:scrollView];
    
    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, scrollView.frame.size.width, 200)];
    redView.backgroundColor = [UIColor redColor];
    [scrollView addSubview:redView];
    self.scrollView = scrollView;
    
    YCDynamicNavigationBar *bar = [[YCDynamicNavigationBar alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, 64)];
    bar.delegate = self;
    bar.titleLb.text = @"我是标题";
    bar.threshold = 200 - 64;
    bar.hideBottomLine = YES;
    [bar bindScrollView:scrollView];
    [bar addLeftTitle:@"返回" image:nil target:nil action: nil];
    [bar addRightTitle:@"更多" image:nil target:nil action:nil];
    self.bar = bar;
    [self.view addSubview:bar];
}

#pragma mark - YCDynamicNavigationBarDelegate

- (void)dynamicBar:(YCDynamicNavigationBar *)bar didChangeHeight:(CGFloat)height {
    NSLog(@"didChangeHeight: %f", height);
//    CGRect rect = self.scrollView.frame;
//    rect.origin.y = height;
//    rect.size.height = kSCREEN_HEIGHT - height;
//    self.scrollView.frame = rect;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat alpha = MIN(scrollView.contentOffset.y / 40, 1);
    self.bar.backgroundColor = [UIColor colorWithRed:0.298 green:0.569 blue:0.929 alpha:alpha];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
